﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManager.BusinessLogic.Abstract;
using HotelManager.DataAccess.Model;

namespace HotelManager.WebUI.Controllers
{
    public class HomeController : Controller
    {
        private ILocationService _locationService;

        public HomeController(ILocationService service)
        {
            _locationService = service;
        }

        public ActionResult Index()
        {
            SearchModel model = new SearchModel();
            SelectList list = new SelectList(_locationService.GetAllCountries(), "Id", "Name");
            ViewBag.CountryId = list;
            return View(model);
        }

        public JsonResult GetCities(int countryId)
        {
            var list = _locationService.GetCitiesByCountry(countryId).Select(x => new { x.Id, x.Name });
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        #region Validation Rules

        public JsonResult ValidateCheckoutTime(DateTime CheckOutTime, DateTime CheckInTime)
        {
            if (CheckOutTime < CheckInTime)
            {
                return Json("Invalid time!", JsonRequestBehavior.AllowGet);
            }
            return Json(true, JsonRequestBehavior.AllowGet);
        }

        public JsonResult ValidateCheckinTime(DateTime CheckInTime)
        {
            if (CheckInTime < DateTime.Today)
            {
                return Json("Invalid time!", JsonRequestBehavior.AllowGet);
            }
            return Json(true, JsonRequestBehavior.AllowGet);
        }

        #endregion

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Contact()
        {
            ContactModel model = new ContactModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Contact(ContactModel model)
        {
            
            if (ModelState.IsValid)
            {
                _locationService.AddContact(model);
                return View(new ContactModel()
                {
                    reply = "Contact successfully"
                });
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }
    }
}
